package br.com.ss.centralaamar.component;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class EmailTest {

	public static void main(String[] args) throws EmailException, MalformedURLException {
		
		final String usuario = "alavaraesam@gmail.com";
		final String senha = "claudemir";
		HtmlEmail email = new HtmlEmail();
		  email.setHostName("smtp.gmail.com");
		  email.addTo("claudemirramosferreira@gmail.com", "John Doe");
		  email.setFrom("claudemirramosferreira@gmail.com", "Me");
		  email.setSmtpPort(465);
		  email.setAuthentication("claudemirramosferreira@gmail.com", "vedita1234");
		  email.setSubject("Test envio de e-mail");
		  email.setDebug(true);
				  
		  Properties mailProps = new Properties();
			mailProps.put("mail.transport.protocol", "smtp");
			mailProps.put ("mail.smtp.starttls.enable", "true");
	        mailProps.put("mail.smtp.starttls.enable","true");
	        mailProps.put("mail.smtp.host", "smtp.gmail.com");
	        mailProps.put("mail.smtp.auth", "true");
	        mailProps.put("mail.smtp.user", usuario);
	        mailProps.put("mail.debug", "true");
	        mailProps.put("mail.smtp.port", "465");
	        mailProps.put("mail.smtp.socketFactory.port", "465");
	        mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	        mailProps.put("mail.smtp.socketFactory.fallback", "false");

	        //eh necessario autenticar
			Session mailSession = Session.getInstance(mailProps, new Authenticator() {					
				public PasswordAuthentication getPasswordAuthentication(){				
					return new PasswordAuthentication(usuario, senha);
				}
			});

		  
		  // embed the image and get the content id
		  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
		  String cid = email.embed(url, "Apache logo");
		  
		  // set the html message
		  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

		  // set the alternative message
		  email.setTextMsg("Your email client does not support HTML messages");

		  // send the email
		  email.send();
		  System.out.println("email enviado");
	}
}
